<template>
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in rolesList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="doSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { getRoles, updatedRoles } from '@/api/settings'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      roleIds: [],
      rolesList: []
    }
  },
  created() {
    this.loadRolesById()
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('hSuccess')
    },
    async  loadRolesById() {
      const res = await getUserDetailById(this.curId)
      this.roleIds = res.data.roleIds
      console.log(res)
    },
    async  loadRoles() {
      const res = await getRoles({ size: 1, pagesize: 1000 })
      this.rolesList = res.data.rows
    },
    // 点击确认按钮
    async doSubmit() {
      await updatedRoles({ id: this.curId, roleIds: this.roleIds })
      this.$message.success('更新权限成功')
      this.$emit('hSuccess')
    }
  }
}
</script>
